<?php

require_once 'opendb.php';
require_once 'assignment.php';
require_once 'course.php';
require_once 'weightManager.php';


class assignmentManager
{
    public static function addAssignment($assignment)
    {
        $assignment = mysql_real_escape_string($assignment);
        $userID = $assignment->getUserID();
        $classID = $assignment->getClassID();
        $name = $assignment->getName();
        $description = $assignment->getDescription();
        $dateDue = $assignment->getDateDue();
        $earned = $assignment->getEarnedPoints();
        $potent = $assignment->getPotentialPoints();
        $alert = $assignment->getAlert();
        $isComplete = $assignment->getIsComplete();
        $weight = $assignment->getWeight();
        $insert = mysql_query("INSERT INTO assignments VALUES('$userID', '$classID', NULL, '$name', '$description', '$dateDue', '$earned', '$potent', '$alert', '$isComplete', '$weight')") or die(mysql_error());
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The assignment has been added successfully.";
        else
            echo "The assignment has not been added successfully.";
    }

    public static function editAssignment($assignment)
    {
        $assignemnt = mysql_real_escape_string($assignment);
        $assignID = $assignment->getAssignID();
        $userID = $assignment->getUserID();
        $classID = $assignment->getClassID();
        $name = $assignment->getName();
        $description = $assignment->getDescription();
        $dateDue = $assignment->getDateDue();
        $earned = $assignment->getEarnedPoints();
        $potent = $assignment->getPotentialPoints();
        $alert = $assignment->getAlert();
        $isComplete = $assignment->getIsComplete();
        $weight = $assignment->getWeight();
        $update = mysql_query("UPDATE assignments SET UserID = '$userID', CourseID = '$classID', Name = '$name', Description = '$description', DueDate = '$dateDue', EarnedPoints = '$earned', PotentialPoints = '$potent', AlertBool = '$alert', IsCompleteBool = '$isComplete', weightID = '$weight' WHERE AssignmentID = '$assignID'") or die(mysql_error());
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The assignment has been edited successfully.";
        else
            echo "The assignment has not been edited successfully.";

    }

    public static function deleteAssignment($assignment)
    {

        $assignment = mysql_real_escape_string($assignment);
        $delete = mysql_query("DELETE FROM assignments WHERE AssignmentID = '$assignment->getAssignID()' LIMIT 1");
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The assignment has been successfully deleted.";
        else
            echo "The assignment has not been successfully deleted.";
    }

    public static function getIncompleteAssignments($userID)
    {
        $userID = mysql_real_escape_string($userID);
        $select = mysql_query("SELECT * FROM assignments WHERE UserID = '$userID' AND IsCompleteBool = '0'");
        $result = mysql_num_rows($select);
        $assignArray = array();
        while ($row = mysql_fetch_array($select))
        {
            $assign = new Assignment();
            $assign = assignmentManager::getAssignment($row['AssignmentID']);
            $assignArray[] = $assign;
        }
        return $assignArray;
    }

    public static function getAllAssignments($userID)
    {
        $userID = mysql_real_escape_string($userID);
        $select = mysql_query("SELECT * FROM assignments WHERE UserID = '$userID'");
        $result = mysql_num_rows($select);
        $assignArray = array();
        while ($row = mysql_fetch_array($select))
        {
            $assign = new Assignment();
            $assign = assignmentManager::getAssignment($row['AssignmentID']);
            $assignArray[] = $assign;
        }
        return $assignArray;
    }

    public static function getCourseAssignments($course)
    {
        //$course = mysql_real_escape_string($course);
        $userID = $course->getUserID();
        $courseID = $course->getID();
        $name = $course->getName();
        $select = mysql_query("SELECT * FROM assignments WHERE UserID = '$userID' AND CourseID = '$courseID'") or die(mysql_error());
        $result = mysql_num_rows($select);
        $assignArray = array();
        while ($row = mysql_fetch_array($select))
        {
            $assign = new Assignment();
            $assign = assignmentManager::getAssignment($row['AssignmentID']);
            $assignArray[] = $assign;
        }
        return $assignArray;
    }

    public static function getAssignment($assignID)
    {
        $assignID = mysql_real_escape_string($assignID);
        $select = mysql_query("SELECT * FROM assignments WHERE assignmentID = '$assignID'");
        $result = mysql_num_rows($select);
        if ($result > 0)
        {
            $assignInfo = mysql_fetch_array($select);
            $assign = new Assignment();
            $assign->setUserID($assignInfo['UserID']);
            $assign->setClassID($assignInfo['CourseID']);
            $assign->setAssignID($assignInfo['AssignmentID']);
            $assign->setName($assignInfo['Name']);
            $assign->setDescription($assignInfo['Description']);
            $assign->setDateDue($assignInfo['DueDate']);
            $assign->setEarnedPoints($assignInfo['EarnedPoints']);
            $assign->setPotentialPoints($assignInfo['PotentialPoints']);
            $assign->setAlert($assignInfo['AlertBool']);
            $assign->setIsComplete($assignInfo['IsCompleteBool']);
            $assign->setWeight($assignInfo['weightID']);

            return $assign;
        } else
        {
            echo "There is no assignment with that ID.";
        }
    }

    public static function getWeight($weightID)
    {
        $weightID = mysql_real_escape_string($weightID);
        $select = mysql_query("SELECT * FROM course_gradingscale WHERE GradingID = '$weightID'");
        $result = mysql_num_rows($select);
        if ($result > 0)
        {
            $weightInfo = mysql_fetch_array($select);
            return $weightInfo['Percentage'];
        } else {
            echo "There is no weight with that ID";
        }
    }
}

?>
